package com.woniuxy.cloud.userassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.entity.TUser;
import com.woniuxy.cloud.form.AddCouponForm;
import com.woniuxy.cloud.form.RegisterForm;
import com.woniuxy.cloud.userassembly.mapper.TUserMapper;
import com.woniuxy.cloud.userassembly.service.ITUserService;
import com.woniuxy.cloud.userassembly.utils.EnycrptUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-27
 */
@Service
@Slf4j
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    /**
     * 根据用户id查询用户
     * @param userId
     * @return TUser
     */
    @Override
    public TUser getUserByUserId(Integer userId) {
        return getBaseMapper().selectById(userId);
    }



    /**
     * 通过验证码修改密码
     * @param userId 用户id
     * @param code 验证码
     * @param passWord 新密码
     * @return integer
     */
    @Override
    public Integer updateUserPassWord(Integer userId, String code,String passWord) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId).set("user_password",passWord);
        return getBaseMapper().update(null,wrapper);
    }

    /**
     * 根据用户名查询用户
     * @param userName
     * @return TUser
     */
    @Override
    public TUser getUserByUserName(String userName) {
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        return getBaseMapper().selectOne(wrapper);

    }


    /**
     * 根据用户手机号查询用户
     * @param userPhone
     * @return TUser
     */
    @Override
    public TUser getUserByUserPhone(String userPhone) {
        log.info("根据手机号查询用户 手机号={}",userPhone);
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_phone",userPhone);
        return getBaseMapper().selectOne(wrapper);
    }

    /**
     * 注册
     * @param registerForm
     * @return Integer
     */
    @Override
    public TUser register(RegisterForm registerForm) {
        TUser user = new TUser();
        user.setUserName(registerForm.getUserName()).setUserPassword(EnycrptUtils.enycrpt(registerForm.getPassWord())).setUserPhone(registerForm.getPhone()).setUserProfilePhoto("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015a465698b54432f87574be965625.png%401280w_1l_2o_100sh.png&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635577663&t=57119a9b3b7b4badfecf47100dca2b11");
        getBaseMapper().insert(user);
        return user;
    }

    /**
     * 修改用户默认收货地址
     * @param userId
     * @param userAddressId
     * @return Result
     */
    @Override
    public Integer updateDefaultUserAddressByUserId(Integer userId, Integer userAddressId) {
        UpdateWrapper<TUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",userId).set("user_default_address",userAddressId);
        return  getBaseMapper().update(null,updateWrapper);
    }

    /**
     * 修改支付密码
     * @param userId
     * @param newPassword
     * @return
     */
    @Override
    public Integer updatePayPassword(Integer userId,String newPassword) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId).set("user_payment_password",newPassword);
        return getBaseMapper().update(null,wrapper);
    }


    /**
     * 修改积分
     * @param userId
     * @param userScore
     * @return
     */
    @Override
    public Integer updateUserScoreById(Integer userId, Integer userScore) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
         wrapper.eq("user_id",userId).set("user_score",userScore);
        return getBaseMapper().update(null,wrapper);
    }

    @Override
    public Integer updateUserNickName(Integer userId, String newName) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId).set("user_nick_name",newName);
        return getBaseMapper().update(null,wrapper);
    }

    /**
     * 修改用户头像
     * @param userId
     * @param img
     * @return
     */
    @Override
    public Integer updateUserImage(Integer userId,String img) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId).set("user_profile_photo",img);
        return getBaseMapper().update(null,wrapper);
    }

    /**
     * 修改用户钱包余额
     * @param userId 用户id
     * @param balance 修改余额
     * @returnv Integer
     */
    @Override
    public Integer updateUserbalanceByUserId(Integer userId, Double balance) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId).set("user_balance",balance);
        return getBaseMapper().update(null,wrapper);
    }

    /**
     *
     * @param userId 用户id
     * @param balance 消费金额
     * @return Integer
     */
    @Override
    public Integer updateUserPayFeeByUserId(Integer userId, Double balance) {
        UpdateWrapper<TUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",userId).set("user_pay_fee",balance);
        return getBaseMapper().update(null,wrapper);
    }


}
